package com.woniuxy.cloud.carl.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.car.client.CarQueryClient;
import com.woniuxy.cloud.carl.car.client.result.CarForStationDto;
import com.woniuxy.cloud.carl.carlgoods.client.SkuQueryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SkuDto;
import com.woniuxy.cloud.carl.order.client.ServiceManageClient;
import com.woniuxy.cloud.carl.order.client.param.AddServiceParam;
import com.woniuxy.cloud.carl.order.client.param.AddServiceTypeParam;
import com.woniuxy.cloud.carl.order.client.param.ChangeServiceCarParam;
import com.woniuxy.cloud.carl.order.client.param.ModifyServiceParam;
import com.woniuxy.cloud.carl.order.client.result.AddServiceDto;
import com.woniuxy.cloud.carl.order.common.CommonResultCode;
import com.woniuxy.cloud.carl.order.infra.model.*;
import com.woniuxy.cloud.carl.order.service.*;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.CommonResultCodeEnum;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceManageApiImpl implements ServiceManageClient {
    @Autowired
    ServiceService serviceService;
    @Autowired
    SkuQueryClient skuQueryClient;
    @Autowired
    CarQueryClient carQueryClient;
    @Autowired
    CarServiceService carServiceService;
    @Autowired
    FirstTypeService firstTypeService;
    @Autowired
    SecondTypeService secondTypeService;
    @Autowired
    ThirdTypeService thirdTypeService;
    @Override
    public Result<AddServiceDto> addService(AddServiceParam param) {
        Servicee servicee = BeanUtil.copyProperties(param, Servicee.class);
        Assert.state(param.getCarIds()!=null,"车辆信息不能为空");
        //判断车辆是否存在
        for (Long carId : param.getCarIds()) {
            Result<CarForStationDto> detail = carQueryClient.getDetail(carId);
            Assert.state(detail.getData()!=null,"车辆信息错误");
        }
        //判断sku是否存在
        if(servicee.getSkuId()!=null){
            Result<SkuDto> skuById = skuQueryClient.findSkuById(servicee.getSkuId());
            Assert.state(skuById.getData()!=null,"商品信息错误");
        }
        servicee.setServiceStatus(1);
        serviceService.save(servicee);
        for (Long carId : param.getCarIds()) {
            CarService carService = new CarService();
            carService.setCarId(carId);
            carService.setServiceId(servicee.getServiceId());
            carServiceService.save(carService);
        }
        return Result.success();
    }

    @Override
    public Result modifyServiceState(ModifyServiceParam param) {
        Servicee servicee = BeanUtil.copyProperties(param, Servicee.class);
        //判断sku是否存在
        if(servicee.getSkuId()!=null){
            Result<SkuDto> skuById = skuQueryClient.findSkuById(servicee.getSkuId());
            Assert.state(skuById.getData()!=null,"商品信息错误");
        }
        if (param.getServiceStatus()!=null&&(param.getServiceStatus()<0||param.getServiceStatus()>1))
            throw new MallException(CommonResultCodeEnum.ILLEGAL_STATE,"状态信心错误");
        boolean b = serviceService.updateById(servicee);
        if (b) return Result.success();
        return Result.fail();
    }

    @Override
    public Result addServiceCare(ChangeServiceCarParam param) {
        for (Long carId : param.getCarIds()) {
            Result<CarForStationDto> detail = carQueryClient.getDetail(carId);
            Assert.state(detail.getData()!=null,"车辆信息错误");
        }
        for (Long carId : param.getCarIds()) {
            CarService one = carServiceService.getOne(new QueryWrapper<CarService>()
                    .eq("car_id", carId).eq("service_id", param.getServieId()));
            Assert.state(one==null,"重复信息提交");
            CarService carService = new CarService();
            carService.setCarId(carId);
            carService.setServiceId(param.getServieId());
            carServiceService.save(carService);
        }
        return Result.success();
    }

    @Override
    public Result delServiceCar(ChangeServiceCarParam param) {
        for (Long carId : param.getCarIds()) {
            Result<CarForStationDto> detail = carQueryClient.getDetail(carId);
            Assert.state(detail.getData()!=null,"车辆信息错误");
        }
        for (Long carId : param.getCarIds()) {
            carServiceService.remove(new QueryWrapper<CarService>()
                    .eq("car_id",carId).eq("service_id",param.getServieId()));
        }
        return Result.success();
    }

    @Override
    public Result addServiceType(AddServiceTypeParam param) {
        switch (param.getTypeFlag().intValue()){
            case (1):{
                FirstType one = firstTypeService.getOne(new QueryWrapper<FirstType>()
                        .eq("type_name", param.getTypeName()));
                Assert.state(one==null,"请勿重复新增");
                FirstType firstType = new FirstType();
                firstType.setTypeName(param.getTypeName());
                firstTypeService.save(firstType);
                break;
            }
            case (2):{
                SecondType one = secondTypeService.getOne(new QueryWrapper<SecondType>()
                        .eq("second_type_name", param.getTypeName()));
                Assert.state(one==null,"请勿重复新增");
                SecondType secondType = new SecondType();
                secondType.setSecondTypeName(param.getTypeName());
                secondType.setFirstTypeId(param.getParentTypeId());
                secondTypeService.save(secondType);
                break;
            }
            case (3):{
                ThirdType one = thirdTypeService.getOne(new QueryWrapper<ThirdType>()
                        .eq("third_type_name", param.getTypeName()));
                Assert.state(one==null,"请勿重复新增");
                ThirdType thirdType = new ThirdType();
                thirdType.setThirdTypeName(param.getTypeName());
                thirdType.setSecondTypeId(param.getParentTypeId());
                thirdTypeService.save(thirdType);
                break;
            }
            default:{
                break;
            }
        }
        return Result.success();
    }
}
